package cn.tedu.eshihui.controller;

import cn.tedu.eshihui.pojo.dto.AlbumAddNewDTO;
import cn.tedu.eshihui.pojo.dto.AlbumUpdateDTO;
import cn.tedu.eshihui.pojo.vo.AlbumListItemVO;
import cn.tedu.eshihui.pojo.vo.AlbumStandardVO;

import cn.tedu.eshihui.service.impl.AlbumServiceImpl;
import cn.tedu.eshihui.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
@Api(tags = "04. 相册管理模块")
@Slf4j
@RestController
@RequestMapping("/albums")
@Validated
public class AlbumController {

    @Autowired
    private AlbumServiceImpl albumService;


    @PostMapping("/add-new")
    @ApiOperation("添加相册")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid AlbumAddNewDTO albumAddNewDTO) {
        log.debug("开始处理【添加相册】的请求，参数：{}", albumAddNewDTO);
        albumService.addNew(albumAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据id删除相册")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "相册ID", required = true, example = "9527", dataType = "long")
    public JsonResult<Void> delete(@Range(min = 1, max = 1000000, message = "删除相册失败，相册ID非法！")
                                   @PathVariable Long id) {
        log.debug("开始处理【根据id删除相册】的请求，参数：{}", id);
        albumService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改相册详情")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> updateInfoById(@PathVariable Long id, AlbumUpdateDTO albumUpdateDTO) {
        log.debug("开始处理【修改相册详情】的请求，参数：{}", albumUpdateDTO);
        albumService.updateInfoById(id, albumUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据id查询相册详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "id", value = "相册ID", required = true, example = "9527", dataType = "long")
    public JsonResult<AlbumStandardVO> getStandardById(@Range(min = 1, max = 1000000, message = "查询相册详情失败，相册ID非法！")
                                                       @PathVariable Long id) {
        log.debug("开始处理【根据id查询相册详情】的请求，参数：{}", id);
        AlbumStandardVO albumStandardVO = albumService.getStandardById(id);
        return JsonResult.ok(albumStandardVO);
    }

    @GetMapping("list")
    @ApiOperation("查询相册列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<AlbumListItemVO>> list() {
        log.debug("开始处理【查询相册列表】的业务，无参数");
        List<AlbumListItemVO> list = albumService.list();
        return JsonResult.ok(list);
    }

    @PostMapping("select")
    @ApiOperation("查询相册列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<AlbumStandardVO>> selectByName() {
        log.debug("开始处理【查询相册列表】的业务，无参数");
        List<AlbumStandardVO> list = albumService.selectByName();
        return JsonResult.ok(list);
    }

    @Deprecated
    @GetMapping("/{name:[a-z]+}")
    public String getStandardByName(@PathVariable String name) {
        return "根据Name【" + name + "】查询相册详情";
    }

    // http://localhost:9080/albums/test
    @Deprecated
    @GetMapping("/test")
    public String testStandardByName(String name) {
        return "【测试】根据Name【" + name + "】查询相册详情";
    }
}
